package cloud.estate.activity.service.impl;

import cloud.estate.activity.service.ActivityService;
import cloud.estate.commons.bean.Activity;
import cloud.estate.commons.handler.exception.*;
import cloud.estate.commons.mapper.ActivityMapper;
import cloud.estate.commons.util.JsonResult;
import cloud.estate.commons.util.ResultTool;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {
    @Override
    public JsonResult addOneActivity(Activity entity) {
        try {
            boolean save = save(entity);
            if (save) {
                return ResultTool.success("添加成功");
            }
            return ResultTool.fail(501, "添加失败");
        } catch (Exception e) {
            throw new AddOneException(e.getMessage());
        }
    }

    @Override
    public JsonResult updateOneActivity(Activity entity) {
        try {
            boolean update = updateById(entity);
            if (update) {
                return ResultTool.success("修改成功");
            }
            return ResultTool.fail(502, "修改失败");
        } catch (Exception e) {
            throw new UpdateOneException(e.getMessage());
        }
    }

    @Override
    public JsonResult deleteOneActivity(String id) {
        try {
            boolean delete = removeById(id);
            if (delete) {
                return ResultTool.success("删除成功");
            }
            return ResultTool.fail(503, "删除失败");
        } catch (Exception e) {
            throw new DealOneByIdException(e.getMessage());
        }
    }

    @Override
    public JsonResult findOneActivity(String id) {
        try {
            Activity activity = getById(id);
            if (activity != null) {
                return ResultTool.success(activity);
            }
            return ResultTool.fail(504, "查询失败");
        } catch (Exception e) {
            throw new FindOneException(e.getMessage());
        }
    }

    @Override
    public JsonResult findAllActivity() {
        try {
            List<Activity> list = list();
            if (list != null) {
                return ResultTool.success(list);
            }
            return ResultTool.fail(505, "查询失败");
        } catch (Exception e) {
            throw new FindAllException(e.getMessage());
        }
    }
}
